package org.laobai.permission.controller;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

import org.laobai.permission.service.RoleService;
import org.laobai.permission.entity.Role;
import org.laobai.permission.struct.PageResult;
import org.laobai.permission.struct.Result;
import org.laobai.permission.vo.SearchVO;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiusan
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
	private RoleService roleService;

    @GetMapping("/{id}")
    public Result<Role> getById(@PathVariable("id") Long id) {
        return Result.ok(roleService.getById(id));
    }

    @GetMapping
    public Result<List<Role>> list() {
        return Result.ok(roleService.list());
    }

    @GetMapping("getMidsByRid/{rid}")
    public Result<List<Integer>> getMidsByRid(@PathVariable(name = "rid") Integer rid){
        return Result.ok(roleService.getMidsByRid(rid));
    }


    @PostMapping
    public Result add(@RequestBody Role role) {
        return roleService.saveRole(role) ? Result.ok() : Result.error();
    }

    @PutMapping
    public Result update(@RequestBody Role role) {
        return roleService.updateRole(role) ? Result.ok() : Result.error();
    }

    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Long id) {
        return roleService.delById(id) ? Result.ok() : Result.error();
    }

    @PostMapping("search/{currPage}/{pageSize}")
    public PageResult<Role> page(@PathVariable("currPage") Integer currPage,
                                            @PathVariable("pageSize") Integer pageSize,
                                            @RequestBody(required = false) SearchVO vo) {
        return roleService.page(currPage, pageSize, vo);
    }

    @PostMapping("delByIds")
    public Result delByIds(@RequestBody List<Long> ids) {
        return roleService.delByIds(ids) ? Result.ok() : Result.error();
    }

}
